package edu.sjsu.cs.queryopt.dbms.main;

import java.util.ArrayList;
import java.util.Hashtable;

public class AccessTypeFour {
	/**
	 * It is class that only case of SELECT COLS FROM TABLE
	 * */
	static SelectHelper sh = new SelectHelper();
	static AccessTypeOne ato = new AccessTypeOne();
		
	  public  void AccessTypeFour(ArrayList<String> outputContainer , ArrayList<String> conditionpod , ArrayList<String> frontColpod)
	    {
		  
//		  System.out.println("/-----ACCESS ONE-----/");
//			System.out.println("outputContainer " + outputContainer);
//			System.out.println("conditionpod " + conditionpod);
//			System.out.println("frontColpod " + frontColpod);
			
			Hashtable<String, ArrayList<String>> ht = new Hashtable<String,ArrayList<String>>();
			ArrayList<String> KeyList = new ArrayList<String>();
			
			ArrayList<String> IndexList = new ArrayList<String>();
			IndexList= sh.StringToArrayList(outputContainer);
	    	ArrayList<String> ColCon = new ArrayList<String>();
	    	ArrayList<Integer> remember = new ArrayList<Integer>();
	    	for(int index = 0 ; index < IndexList.size() ; index++){
	    		if(IndexList.get(index).startsWith("T")){
	    			KeyList.add(IndexList.get(index));
	    			remember.add(index);
	    		}
	    	}
	    	remember.add(IndexList.size());
//	    	System.out.println("KeyList " + KeyList);
	    	for(int in = 0 ; in < KeyList.size() ; in++)
	    	{
	    		for(int out = remember.get(in) + 1 ; out < remember.get(in+1) ; out++)
	    		{
	    			
	    			ColCon.add(IndexList.get(out));
	    		}
	    		ht.put(KeyList.get(in), ColCon);
	    		ColCon = new ArrayList<String>();
	    	}
	    	ArrayList<String> allindex = ato.getTableList(conditionpod , frontColpod);
//	    	System.out.println("ht " + ht);
//	    	System.out.println("allindex " + allindex);
	    	indexOrIndexScan(ht,KeyList,  allindex);
	    }
		/*
		 * Index only can after where and after select
		 * */
		public int indexOrIndexScan( Hashtable<String, ArrayList<String>> ht,  ArrayList<String> keyset,  ArrayList<String> allindex){
			
			int counter = 0;
			ArrayList<String> newSet = new ArrayList<String>();
			Hashtable <String, Integer> htc = new Hashtable <String, Integer> ();
			for(int index = 0 ; index < ht.size() ; index++){
				ArrayList<String> htlist = ato.getRidOfA(ht.get(keyset.get(index)));
//				System.out.println("htlist " + htlist);
				for(int in = 0 ; in < allindex.size() ; in ++){
					if(keyset.get(index).trim().startsWith(ato.getTableName(allindex.get(in)).trim())){
//						System.out.println("keyset.get(index) " + keyset.get(index) + " contians  " +  allindex.get(in));
						for(int ii = 0 ; ii < allindex.size() ; ii++){
//							System.out.println("getColNumer(allindex.get(ii).trim()).trim()) " +ato. getColNumer(allindex.get(ii).trim()).trim());
							if(ato.IsIntheList(htlist ,ato.getColNumer(allindex.get(ii).trim()).trim())){
//								System.out.println("FOUNRD");
								counter ++;
							}
						}
//						System.out.println("counter " + counter);
						htc.put(keyset.get(index), counter);
						newSet.add(keyset.get(index));
						counter = 0;
					}
				}
				
			}
			
//			System.out.println("htC " + htc);
			IsIndexOnly(htc , newSet , allindex );

			
			return 0;
			
		}
		public int IsIndexOnly(Hashtable<String, Integer> ht ,ArrayList<String> keyset , ArrayList<String> allindex){
			
			int bigger = 0;
			int remember = 0;
			for(int index = 0 ; index < keyset.size() ; index++){
//				System.out.println("ht.get(keyset.get(index))  " + ht.get(keyset.get(index)) );
				if(ht.get(keyset.get(index)) > bigger){
					bigger = ht.get(keyset.get(index));
					remember = index;
				}
				if(ht.get(keyset.get(index)) == allindex.size()){
					bigger = 99;
					remember = index;
					break;
					
				}
				
				
			}
//			System.out.println("dfasdfa ? " + allindex);
			if(bigger == 0){
//				System.out.println("R-SCAN");
				DBMS.PlanTable.add("R");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add("S");
				DBMS.PlanTable.add("N");
				return 1;
			}
			else if(bigger == 99){
//				System.out.println("INDEX ONLY");
//				System.out.println("THIS INDEX LIST " + keyset.get(remember));
				DBMS.PlanTable.add("I");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add(keyset.get(remember));
				DBMS.PlanTable.add("Y");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add("N");
				return 2;
				
			}
			else{
//				System.out.println("INDEX SCAN BUT NOT INDEX ONLY");
//				System.out.println("THIS INDEX LIST " + keyset.get(remember));
				DBMS.PlanTable.add("I");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add(keyset.get(remember));
				DBMS.PlanTable.add("N");
				DBMS.PlanTable.add(" ");
				DBMS.PlanTable.add("N");
				
				return 3;
				
			}
			
			
			
		}
		
}
